import { defineStore } from 'pinia'
import { resHasAttributeCate } from '@/api/product'

import type { AttributeCateStore } from './types/type'
import type { AttributeCateResponseData } from '@/api/product/type'
const useAttributeCate = defineStore('AttributeCate', {
  state(): AttributeCateStore {
    return {
      key_first: '',
      key_second: '',
      key_third: '',
      option_first: [],
      option_second: [],
      option_third: [],
    }
  },
  actions: {
    async GET_CATE({ level = 1, id = 0 } = {}) {
      const result: AttributeCateResponseData = await resHasAttributeCate({
        level,
        id,
      })
      if (result.code == 200) {
        this.SET_OPTION({ level, data: result.data })
      }
    },
    SET_OPTION({ level, data }) {
      if (level == 1) {
        this.option_first = data
      } else if (level == 2) {
        this.option_second = data
      } else if (level == 3) {
        this.option_third = data
      }
    },
    RECORD_SLECET(Keys: number[]) {
      for (let index = 0; index < Keys.length; index++) {
        const element = Keys[index]
        if (index == 2) break
        this.GET_CATE({ level: index + 2, id: element })
      }
      this.key_first = Keys[0]
      this.key_second = Keys[1]
      this.key_third = Keys[2]
    },
  },
  getters: {},
})

export default useAttributeCate
